<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户手机" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入用户手机"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出发地址" prop="startingPoint">
        <el-input
          v-model="queryParams.startingPoint"
          placeholder="请输入出发地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目的地址" prop="destination">
        <el-input
          v-model="queryParams.destination"
          placeholder="请输入目的地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单费用" prop="orderPrice">
        <el-input
          v-model="queryParams.orderPrice"
          placeholder="请输入订单费用"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款编号" prop="payId">
        <el-input
          v-model="queryParams.payId"
          placeholder="请输入付款编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款时间" prop="payTime">
        <el-date-picker clearable
          v-model="queryParams.payTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择付款时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="骑行车辆" prop="vehicleCode">
        <el-input
          v-model="queryParams.vehicleCode"
          placeholder="请输入骑行车辆"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请选择商户"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['order:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['order:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="用户手机" align="center" prop="mobile" />
      <el-table-column label="状态" align="center" prop="orderStatus">
        <template #default="scope">
          <dict-tag :options="order_status" :value="scope.row.orderStatus" />
        </template>
      </el-table-column>
      <el-table-column label="出发地址" align="center" prop="startingPoint" />
      <el-table-column label="出发时间" align="center" prop="startTime" width="180"/>
      <el-table-column label="目的地址" align="center" prop="destination" />
      <el-table-column label="到达时间" align="center" prop="endTime" width="180"/>
      <el-table-column label="总时长" align="center" prop="orderMins" />
      <el-table-column label="订单费用" align="center" prop="orderPrice" />
      <el-table-column label="付款编号" align="center" prop="payId" />
      <el-table-column label="付款时间" align="center" prop="payTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="骑行车辆" align="center" prop="vehicleCode" />
      <el-table-column label="商户ID" align="center" prop="deptId" />
      <el-table-column label="商户名称" align="center" prop="deptName" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.deptName, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单留言" align="center" prop="message" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['order:order:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['order:order:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="orderRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="orderNo"  v-if="form.orderNo" >
          <el-input v-model="form.orderNo"/>
        </el-form-item>
        <el-form-item label="用户手机号" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="用户手机" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入用户手机" />
        </el-form-item>
        <el-form-item label="出发地址" prop="startingPoint">
          <el-input v-model="form.startingPoint" placeholder="请输入出发地址" />
        </el-form-item>
        <el-form-item label="目的地址" prop="destination">
          <el-input v-model="form.destination" placeholder="请输入目的地址" />
        </el-form-item>
        <el-form-item label="用户订单留言" prop="message">
          <el-input v-model="form.message" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="订单费用" prop="orderPrice">
          <el-input v-model="form.orderPrice" placeholder="请输入订单费用" />
        </el-form-item>
        <el-form-item label="付款编号" prop="payId">
          <el-input v-model="form.payId" placeholder="请输入付款编号" />
        </el-form-item>
        <el-form-item label="付款时间" prop="payTime">
          <el-date-picker clearable
            v-model="form.payTime"
            type="date"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择付款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="骑行车辆" prop="vehicleCode">
          <el-input v-model="form.vehicleCode" placeholder="请输入骑行车辆" />
        </el-form-item>
        <el-form-item label="商户名称" prop="deptName">
          <el-date-picker clearable
            v-model="form.deptName"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择商户名称">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Order">
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/order/order";

const { proxy } = getCurrentInstance();
const { order_status, user_violation_type } = proxy.useDict("order_status", "user_violation_type");
const orderList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNo: null,
    userId: null,
    orderStatus: null,
    mobile: null,
    startingPoint: null,
    destination: null,
    message: null,
    goodsPrice: null,
    servicePrice: null,
    couponPrice: null,
    integralPrice: null,
    grouponPrice: null,
    orderPrice: null,
    actualPrice: null,
    payId: null,
    payTime: null,
    vehicleCode: null,
    deptId: null,
    deptName: null,
    settlementStatus: null,
    freightType: null,
  },
  rules: {
    orderNo: [
      { required: true, message: "订单编号不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "用户ID不能为空", trigger: "blur" }
    ],
    orderStatus: [
      { required: true, message: "订单状态不能为空", trigger: "change" }
    ],
    orderPrice: [
      { required: false, message: "订单费用不能为空", trigger: "blur" }
    ],
    actualPrice: [
      { required: true, message: "实付费用不能为空", trigger: "blur" }
    ],
    settlementStatus: [
      { required: true, message: "结算状态不能为空", trigger: "change" }
    ],
    freightType: [
      { required: true, message: "配送方式 ：0 快递, 1 自提不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询订单信息列表 */
function getList() {
  loading.value = true;
  listOrder(queryParams.value).then(response => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    orderNo: null,
    userId: null,
    orderStatus: null,
    mobile: null,
    startTime: null,
    startingPoint: null,
    endTime: null,
    destination: null,
    orderMins: null,
    message: null,
    goodsPrice: null,
    servicePrice: null,
    couponPrice: null,
    integralPrice: null,
    grouponPrice: null,
    orderPrice: null,
    actualPrice: null,
    payId: null,
    payTime: null,
    vehicleCode: null,
    deptId: null,
    deptName: null,
    settlementStatus: null,
    freightType: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("orderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.orderNo);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加订单信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _orderNo = row.orderNo || ids.value
  getOrder(_orderNo).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改订单信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderRef"].validate(valid => {
    if (valid) {
      if (form.value.orderNo != null) {
        updateOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _orderNos = row.orderNo || ids.value;
  proxy.$modal.confirm('是否确认删除订单信息编号为"' + _orderNos + '"的数据项？').then(function() {
    return delOrder(_orderNos);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('order/order/export', {
    ...queryParams.value
  }, `order_${new Date().getTime()}.xlsx`)
}

getList();
</script>
